//
//  MCSettingVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/5.
//

import UIKit
import SnapKit
import QMUIKit

class MCSettingVc: QMUICommonTableViewController {
    
    let settingidentifier:String = "settingidentifier"
    let settingSwitchIdentifier:String = "settingSwitchIdentifier"
    
    var dataSource:Array = ["账户设置","用户协议","隐私协议","关于Master","注销账号"]
    
    var titleDataSource:Array = ["训练提醒","动作更新"]
    
    
    var userProfile:MCUserProfileModel!
    
    override func didInitialize() {
        super.didInitialize()
        self.title = "设置"
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        
        self.tableView.register(MCSettingTableCell.self, forCellReuseIdentifier: settingidentifier)
        
        self.tableView.register(MCSettingSwitchTableViewCell.self, forCellReuseIdentifier: settingSwitchIdentifier)
       
        self.tableView.separatorStyle = .none
        
        self.tableView.rowHeight = 54
     
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return self.titleDataSource.count
        }else if section == 1 {
            return self.dataSource.count
        }else {
            return 0
        }
       
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 0.1
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 0 {
            return 10.0
        }else if section == 1 {
            return 90
        }else {
            return 0.1
        }
        
    }
    
     
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell:MCSettingSwitchTableViewCell = tableView.dequeueReusableCell(withIdentifier: settingSwitchIdentifier) as! MCSettingSwitchTableViewCell
            cell.titleSettingLabel.text = self.titleDataSource[indexPath.row]
            return cell
        }else if indexPath.section == 1 {
            let cell:MCSettingTableCell = tableView.dequeueReusableCell(withIdentifier: settingidentifier) as! MCSettingTableCell
            cell.titleSetttingTableLabel.text = self.dataSource[indexPath.row]
            return cell
        }else{
            let cell:MCSettingTableCell = tableView.dequeueReusableCell(withIdentifier: settingidentifier) as! MCSettingTableCell
            
            return cell
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 {
            if indexPath.row == 0 {
                let accountSettingVc = MCAccountSettingVc.init()
                accountSettingVc.userProfile = self.userProfile
                self.navigationController?.pushViewController(accountSettingVc, animated: true)
            }else if indexPath.row == 1 {
                let privacryVc = MCUserPricityVc.init()
                self.navigationController?.pushViewController(privacryVc, animated: true)
            }else if indexPath.row == 2{
                let privacryVc = MCWebProtolVc.init()
                self.navigationController?.pushViewController(privacryVc, animated: true)
            }else if indexPath.row == 3 {
                let aboutVc = MCAboutVc.init()
                self.navigationController?.pushViewController(aboutVc, animated: true)
            }else if indexPath.row == 4{
                let logOffVc = MCLogOffVc.init()
                self.navigationController?.pushViewController(logOffVc, animated: true)
            }
            
        }
    }
    
    
    
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if section == 0 {
            return UIView.init()
        }else if section == 1 {
        
            let logoutView = UIView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 90))
            logoutView.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
            
            let logutBtn = QMUIButton.init()
            logutBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 15)
            logutBtn.setTitle("退出登录", for: .normal)
            logutBtn.setTitleColor(UIColor.qmui_color(withHexString: "#333333"), for: .normal)
            logutBtn.backgroundColor = UIColor.qmui_color(withHexString: "#FFFFFF")
            logutBtn.layer.cornerRadius = 8
            logutBtn.layer.masksToBounds = true
            logutBtn.addTarget(self, action: #selector(logoutLogin), for: .touchUpInside)
            logoutView.addSubview(logutBtn)
            logutBtn.snp.makeConstraints { make in
                make.left.equalTo(logoutView.snp.left).offset(16)
                make.right.equalTo(logoutView.snp.right).offset(-16)
                make.top.equalTo(logoutView.snp.top).offset(20)
                make.height.equalTo(49)
            }
            
            return logoutView
                        
        }else  {
            return UIView.init()
        }
    }
    
    @objc func logoutLogin() -> Void{
        let actionCancel = QMUIAlertAction.init(title: "取消", style: QMUIAlertActionStyle.cancel)
        let actionLogout = QMUIAlertAction.init(title: "确定", style: .default) { alertController, alertAction in
            self.logOut()
            UserDefaults.standard.removeObject(forKey: "LoginToken")
            UserDefaults.standard.removeObject(forKey: "LoginName")
            let appdelete:AppDelegate = UIApplication.shared.delegate as! AppDelegate
            appdelete.Login()
        }
        
        let alerController = QMUIAlertController(title: "确定退出", message: "将推出现有的页面", preferredStyle: .alert)
        alerController.addAction(actionCancel)
        alerController.addAction(actionLogout)
        alerController.showWith(animated: true)
        
    }
    
    
    func logOut() -> Void {
        NetWorkRequest(MCAccountApi.logOut) { ResponseModel in
            if ResponseModel.code == 200 {
                
            }else{
                self.view.makeToast("退出失败",position: .center)
            }
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("退出失败",position: .center)
        }
    }
    
    
}
